﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Library
{
    public class Screen
    {
        public CompCollection Components;
        public bool Visible = true;
        public bool StopsUpdate = false;
        public bool StopsDraw = false;
        public bool StopsInput = false;
        public bool OverridesStoppedUpdate = false;
        public bool OverridesStoppedDraw = false;
        public bool OverridesStoppedInput = false;
        public bool InputDisabled = false;
        public bool IsInputAllowed = true;
        public string Name;

        public event EventHandler OnInit;

        bool initialized = false;

        public bool Initialized 
        {
            get { return initialized; }
            set 
            {
                initialized = value;
                if (OnInit != null)
                {
                    OnInit(this, new EventArgs());
                }
            }
        }

        public Screen(string Name)
        {
            Components = new CompCollection(this);
            this.Name = Name;
            Engine.Screens.Add(this);
            if (!Initialized)
                Initialize();
        }

        public virtual void Initialize()
        {
            this.Initialized = true;
        }

        public virtual void Update()
        {
            List<Component> updating = new List<Component>();

            foreach (Component c in Components)
                updating.Add(c);

            foreach (Component Cp in updating)
                if (Cp.Initialized)
                    Cp.Update();
        }

        public virtual void Draw(ComponentType RenderType)
        {
            List<Component> drawing = new List<Component>();

            foreach (Component cp in Components.InDrawOrder)
            {
                if (RenderType == ComponentType.Both)
                {
                    if (cp is I2DComponent || cp is I3DComponent)
                        drawing.Add(cp);
                }
                else if (RenderType == ComponentType.Component2D)
                {
                    if (cp is I2DComponent)
                        drawing.Add(cp);
                }
                else if (RenderType == ComponentType.Component3D)
                {
                    if (cp is I3DComponent)
                        drawing.Add(cp);
                }
                else
                {
                    drawing.Add(cp);
                }
            }

            List<Component> def2D = new List<Component>();

            foreach (Component cp in drawing)
                if (cp.Visible && cp.Initialized)
                {
                    if (cp is I2DComponent)
                    {
                        def2D.Add(cp);
                    }
                    else
                    {
                        cp.Draw();
                    }
                }
            foreach (Component cp in def2D)
                cp.Draw();
        }

        public virtual void Disable()
        {
            Components.Clear();
            Engine.Screens.Remove(this);
            if (Engine.DefScreen == this)
                Engine.DefScreen = Engine.BgScreen;
        }

        public override string ToString()
        {
            return Name;
        }
    }
}